#include "mycoin.h"
#include<QDebug>
#include<QTimer>
//MyCoin::MyCoin(QWidget *parent) : QWidget(parent)
//{

//}

//重写MyCoin构造函数
MyCoin::MyCoin(QString btnImg){
    QPixmap pix;
    bool ret=pix.load(btnImg);
    if(!ret){
        QString str=QString ("tupian %1 Error").arg(btnImg);

        qDebug()<<str;
        return;
    }

    //日常设置大小
    this->setFixedSize(pix.width(),pix.height());
    this->setStyleSheet("QPushButton{vorder:0px;}");
    this->setIcon(pix);
    this->setIconSize(QSize(pix.width(),pix.height()));

    //初始化定时器
    timer1=new QTimer(this);
    timer2=new QTimer(this);

    //监听正面翻反面的信号槽,并且反转金币
    connect(timer1,&QTimer::timeout,[=](){

        QPixmap pixmap;
        QString str=QString(":/res/Coin000%1.png").arg(this->min++);
        pixmap.load(str);
        this->setFixedSize(pixmap.width(),pixmap.height());
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pixmap);
        this->setIconSize(QSize(pixmap.width(),pixmap.height()));

        //判断，如果翻完了，将min置为1；
        if(this->min>this->max){
            this->min=1;
            isAnimation=false;
            timer1->stop();
        }
    });

}

void MyCoin::changeFlag()
{
    if(this->flag){
        timer1->start(30);
        isAnimation=true;
        this->flag=false;
    }
    else{
        timer2->start(30);
        isAnimation=true;
        this->flag=true;
    }
}

void MyCoin::mousePressEvent(QMouseEvent *e)
{
    if(this->isAnimation||this->isWin){
        return;
    }
    else{
        QPushButton::mousePressEvent(e);
    }
}
